package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.base.web.utils.BaseAction;
import cn.itcast.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

    @Autowired
    private FixedAreaService fixedAreaService;

    @Action(value = "fixed_area_save", results = {@Result(name = "success", type = "redirect", location = "./pages/base/fixed_area.html")})
    public String save() {
        fixedAreaService.save(model);
        return SUCCESS;
    }

    @Action(value = "fixed_area_pageQuery", results = {@Result(name = "success", type = "json")})
    public String pageQuery() {

        Specification<FixedArea> spec = new Specification<FixedArea>() {

            @Override
            public Predicate toPredicate(Root<FixedArea> root,
                                         CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (StringUtils.isNotBlank(model.getId())) {
                    Predicate p1 = cb.equal(root.get("id").as(String.class),
                        model.getId());
                    list.add(p1);
                }
                if (StringUtils.isNotBlank(model.getCompany())) {
                    Predicate p1 = cb.like(
                        root.get("company").as(String.class),
                        model.getCompany());
                    list.add(p1);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };

        Pageable pageable = new PageRequest(page - 1, rows);

        Page<FixedArea> pageData = fixedAreaService.find(spec, pageable);

        PagePushIntoValueStack(pageData);
        return SUCCESS;
    }

	/*@Action(value="fixedArea_findCustomer",results = {@Result(name="success",type="json")})
    public String findCustomer(){

		Collection<? extends Customer> collection = WebClient.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers").accept(MediaType.APPLICATION_JSON_TYPE)
			.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}*/

    @Action(value = "fixed_area_associations", results = {@Result(name = "success", type = "json")})
    public String associations() {

        Collection<? extends Customer> collection = WebClient.create("http://localhost:9001/crm_management/services/customerService/findAssociationFixedAreaCustomer/" + model.getId()).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
        ActionContext.getContext().getValueStack().push(collection);
        return SUCCESS;
    }
    @Action(value = "fixed_area_noAssociations", results = {@Result(name = "success", type = "json")})
    public String noassociations() {

        Collection<? extends Customer> collection = WebClient.create("http://localhost:9001/crm_management/services/customerService/findNoAssociationFixedAreaCustomer").accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
        ActionContext.getContext().getValueStack().push(collection);
        return SUCCESS;
    }

    private String[] customerIds;

    public void setCustomerIds(String[] customerIds) {
        this.customerIds = customerIds;
    }

    @Action(value = "fixed_area_associationCustomerToFixedArea",results = {@Result(name = "success",type = "redirect",location = "./pages/base/fixed_area.html")})
    public String fixed_area_associationCustomerToFixedArea(){
        String customerIdStr = StringUtils.join(customerIds, ",");
        WebClient.create("http://localhost:9001/crm_management/services/customerService/associationCustomerToFixedArea?customerIdStr="+customerIdStr+"&fixedAreaId="+model.getId()).put(null);
        return SUCCESS;
    }

    private String takeTimeId;
    private String courierId;

    public void setTakeTimeId(String takeTimeId) {
        this.takeTimeId = takeTimeId;
    }

    public void setCourierId(String courierId) {
        this.courierId = courierId;
    }

    @Action(value = "fixedArea_associationCourierToFixedArea",results = {@Result(name="success",type = "redirect" ,location = "./pages/base/fixed_area.html")})
    public String associationCourierToFixedArea(){
        fixedAreaService.update(model.getId(),takeTimeId,courierId);
        return SUCCESS;
    }

    @Action(value = "fixed_area_findAll",results = {@Result(name = "success",type = "json")})
    public String findAll(){
       List<FixedArea> list =  fixedAreaService.findAll();
       ActionContext.getContext().getValueStack().push(list);
        return SUCCESS;
    }

}
